﻿using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using AppsGenerator.Generators;
using AppsGenerator.Models;
using AppsGenerator.Utils;
using DBBuilder.DBDataStructure;

namespace AppsGenerator.WebGenerators.WebControllerGenerator
{
    /// <summary>
    /// The algorithm for generating Update method of user controller
    /// </summary>
    [Export(typeof(UpdateControllerItemByUserAlgorithm))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class UpdateControllerItemByUserAlgorithm : BaseGenerator, IUpdateControllerItemAlgorithm
    {
        /// <summary>
        /// The method to generate Update contents of user controller
        /// </summary>
        /// <param name="item">The item which is extracted from template file</param>
        /// <param name="tbl">The datasource for generating</param>
        /// <returns></returns>
        public string GenerateContents(Item item, Table tbl)
        {
            var contents = new StringBuilder(BaseConsts.MAX_BYTES);
            var primaryKey = tbl.Attributes.FirstOrDefault(n => n.IsPrimaryKey);

            foreach (Attribute attr in tbl.Attributes)
            {
                contents.Append(BaseConsts.TAB3).AppendLine(string.Format("var {0} = Forms.Get(\"{1}\");",
                                                                          attr.Name.ToLowerFirstCharacter(), attr.Name));
            }

            contents.Append(BaseConsts.TAB3).AppendLine("string userName = Forms.Get(\"UserName\");");
            contents.Append(BaseConsts.TAB3).AppendLine("string email = Forms.Get(\"Email\");");

            contents.Append(BaseConsts.TAB3).AppendLine(string.Empty);
            contents.Append(BaseConsts.TAB3).AppendLine("var user = Membership.GetUser(userName);");
            contents.Append(BaseConsts.TAB3).AppendLine("if (email != user.Email)");
            contents.Append(BaseConsts.TAB3).AppendLine("{");
            contents.Append(BaseConsts.TAB4).AppendLine("user.Email = email;");
            contents.Append(BaseConsts.TAB4).AppendLine("Membership.UpdateUser(user);");
            contents.Append(BaseConsts.TAB3).AppendLine("}");
            contents.Append(BaseConsts.TAB3).AppendLine(string.Empty);

            contents.Append(BaseConsts.TAB3).AppendLine(string.Format("var item = this.Rep{0}.SelectByID({1});",
                                                                      tbl.Name, this.BuildParsingData(primaryKey)));

            foreach (Attribute attr in tbl.Attributes)
            {
                if (!attr.IsPrimaryKey)
                {
                    contents.Append(BaseConsts.TAB3).AppendLine(string.Format("item.{0}.{1} = {2};", tbl.Name, attr.Name,
                                                                              this.BuildParsingDataWithFirstLowerCharacter(attr)));
                }
            }

            return contents.ToString().TrimStart().TrimEnd();
        }
    }
}
